package cn.aib.eduservice.service.impl;

import cn.aib.eduservice.entity.EduComment;
import cn.aib.eduservice.entity.vo.CommentQueryVo;
import cn.aib.eduservice.mapper.EduCommentMapper;
import cn.aib.eduservice.service.EduCommentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评论 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2021-01-21
 */
@Service
public class EduCommentServiceImpl extends ServiceImpl<EduCommentMapper, EduComment> implements EduCommentService {

    @Override
    public Map<String, Object> getCommentPageInfo(long current, long limit, CommentQueryVo queryVo) {
        //创建page对象
        Page<EduComment> page = new Page<>(current,limit);

        QueryWrapper<EduComment> wrapper = null;

        if(!StringUtils.isEmpty(queryVo)){
            wrapper = new QueryWrapper<>();

            if(!StringUtils.isEmpty(queryVo.getCourseId())){
                wrapper.eq("course_id", queryVo.getCourseId());
            }

            if(queryVo.getIsGmtSort()){
                wrapper.orderByDesc("gmt_create");
            }
        }

        //查询
        baseMapper.selectPage(page, wrapper);

        List<EduComment> records = page.getRecords();

        long currentPage = page.getCurrent();
        long pages = page.getPages();
        long size = page.getSize();
        long total = page.getTotal();
        boolean hasNext = page.hasNext();
        boolean hasPrevious = page.hasPrevious();

        //创建map
        Map<String, Object> map = new HashMap<String, Object>();

        //设置map
        map.put("commentList", records);
        map.put("current", currentPage);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }
}
